<?php

/*

  Action - Attack	$5

  +$2
  Each other player may discard a Curse. If he doesn’t, he gains a Curse and a Copper.

 */

class Mountebank extends Card implements iAction, iKingdom, iAttack, iProsperity {

    public function __construct() {
        parent::__construct("Mountebank", 5);
        $this->undoPlay = false;
        $this->undoBuy = true;
    }

    public function getDescription() {
        return array(
            "+$2",
            "Each other player may discard a Curse. If he doesn’t, he gains a Curse and a Copper.");
    }

    public function play(Game $game, $isRealCard) {


        $game->addMoney(new Price(2));
        ScreenDisplay::addScreenMessage($game, true, "get +2$", "gets +2$");
        $game->setPhase(Phase_Sleep);
        $game->attack($this);

        $names = $game->getOtherPlayersNames(true);
        foreach ($names as $name) {
            $otherPlayHand = $game->getPlayHand($name);
            $curse = $game->getCardInHand("Curse", $name);
            if ($curse) {
                $options = array(new Option(1, "Yes"), new Option(2, "No"));
                $output = MySocket::GetUserReply($game, Input_ChooseOne, $options, 1, 1, null, "Discard a curse?", $name, true);
            } else {
                $curse = $game->moveCardFromPileToPile("Curse", BoardCards, DiscardPile, $name);
                $copper = $game->moveCardFromPileToPile("Copper", BoardCards, DiscardPile, $name);
                $gained_card = array($curse, $copper);
                ScreenDisplay::addScreenMessage($game, true, "gain " . nicePrint($gained_card), "gains " . ScreenDisplay::$LMSG, $name);
            }
        }

        //the waiting function
        $users_replies = MySocket::getUsersReplies($game);
        foreach ($users_replies as $name => $output) {

            $answer = $output[0];
            if ($answer == "Yes") {
                $curse = $game->moveCardFromPileToPile("Curse", PlayHand, DiscardPile, $name);
                ScreenDisplay::addScreenMessage($game, true, "discard ".  nicePrint($curse), "discards ".ScreenDisplay::$LMSG, $name);
            } else if ($answer == "No") {
                $curse = $game->moveCardFromPileToPile("Curse", BoardCards, DiscardPile, $name);
                $copper = $game->moveCardFromPileToPile("Copper", BoardCards, DiscardPile, $name);
                $gained_card = array($curse, $copper);
                ScreenDisplay::addScreenMessage($game, true, "gain " . nicePrint($gained_card), "gains " . ScreenDisplay::$LMSG, $name);
            }

            $game->setPhase(Phase_Sleep, $name);
        }

        $game->cardActionFinished();
        return true;
    }

}

?>